# Time-stamp: <28 Apr 00 16:51:07 giron1>

=head1 NAME

 STATISTICS.pm - some statistics functionality

=head1 DESCRIPTION

This packages contains routines for statistics computation based on
C<Statistics::Descriptive> package.

=head1 SEE ALSO

The package description of B<Statistics::Descriptive>

C<perldoc Statistics::Descriptive>

=head1 AUTHOR

 $Id: STATISTICS.pm,v 1.1 2007/10/08 09:26:04 schweitz Exp $
 Franck Giron

=head1 LOG

 $Log: STATISTICS.pm,v $
 Revision 1.1  2007/10/08 09:26:04  schweitz
 version 2.4, lib vergessen

 Revision 1.1  2000/04/28 14:52:06  giron1
 Pod documentation added


=cut

use strict;
use Statistics::Descriptive;

#----------------------------------------------------------------------

=head1 cost_stat

C<my $stat_hash_ref = cost_stat($file)>

This functions takes as input a I<$file>, coming from a vrecog call (with
the option -print cost), computes the mean value of active states,
number of output probabilities and number of gaussians and output the
results in a hash reference.

=cut

sub cost_stat {
  my $file = shift;

  open (INPUT,"<$file") || die "Cannot read $file";

  #Initialization
  my %param = (state => [], outp => [], gauss => []);
  my $stat_hash_ref = {};
  foreach (keys %param) { $stat_hash_ref->{$_} = Statistics::Descriptive::Sparse->new;}

  # Reads the parameters
  while (<INPUT>) {
    if (m/TCost/) {
      m/state=\s*([0-9]+)\s+outp=\s*([0-9]+)\s+gauss=\s*([0-9]+)/;
      push @{$param{'state'}},$1;
      push @{$param{'outp'}}, $2;
      push @{$param{'gauss'}},$3;
    }
  }
  close(INPUT);

  # Stores and computes the Sparse statistics
  foreach (keys %param) {$stat_hash_ref->{$_}->add_data(@{$param{$_}});}

  return $stat_hash_ref;
}

#----------------------------------------------------------------------

=head1 cost_stat_list

C<cost_stat_list(\@list,$fho);>

This functions takes as input a list I<@list> of log files and
computes the mean value of the active states, the number of output
probabilities and the number of gaussians and output the results in a
comma separated way (Excel format .csv) in the filehandle I<$fho>.

=cut

sub cost_stat_list {
  my $list_ref = shift;
  my $fho = shift;

  # print header
  $fho->print("Costs/frame;Mean;StdDev;Min;Max\n");

  # print statistics for each file of the list
  my ($mean,$stdev,$min,$max,$stat_hash_ref);
  foreach (@$list_ref) {
    $fho->print("File; $_.\n");
    $stat_hash_ref = cost_stat($_);
    foreach (keys %$stat_hash_ref) {
      $mean = int $stat_hash_ref->{$_}->mean + 0.5;
      $stdev = sprintf("%3.2f",$stat_hash_ref->{$_}->standard_deviation);
      $stdev =~ s/\./,/;
      $min = $stat_hash_ref->{$_}->min; $max = $stat_hash_ref->{$_}->max;
      $fho->printf("%s;%ld;%s;%ld;%ld\n",$_,$mean,$stdev,$min,$max);
    }
  }
}
1;
